package com.mentalhealthplatform.teacherV2.service;

import com.mentalhealthplatform.appModels.Student;
import com.mentalhealthplatform.teacherV2.dto.StudentDTO;
import com.mentalhealthplatform.teacherV2.repository.StudentRepositoryV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

// 负责处理与学生相关的业务逻辑
@Service
public class StudentServiceV2 {

    @Autowired
    private StudentRepositoryV2 studentRepositoryV2;

    // 查询所有学生信息并转换为学生DTO
    public List<StudentDTO> findAll() {
        return studentRepositoryV2.findAll().stream()
                .map(StudentDTO::convertToDTO)
                .collect(Collectors.toList());
    }

    // 根据ID查询学生信息
    public Optional<Student> findById(UUID id) {
        return studentRepositoryV2.findById(id);
    }

    // 保存学生信息
    public StudentDTO save(Student student) {
        studentRepositoryV2.save(student);
        return StudentDTO.convertToDTO(student);
    }

    // 根据ID删除特定学生
    public void deleteById(UUID id) {
        studentRepositoryV2.deleteById(id);
    }

    public List<StudentDTO> searchByUsernameOrNumber(String keyword) {
        List<Student> students = studentRepositoryV2.findByNameContainingOrNumberContaining(keyword, keyword);
        return students.stream()
                .map(StudentDTO::convertToDTO)
                .collect(Collectors.toList());
    }
}